package com.zr.influx.controller;

import com.zr.influx.model.ForexDataDTO;
import com.zr.influx.service.ForexQueryService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@RestController
@RequestMapping("/api/forex")
@Tag(name = "外汇行情查询接口", description = "提供外汇行情数据的查询功能")
public class ForexController {

    private final ForexQueryService forexQueryService;

    public ForexController(ForexQueryService forexQueryService) {
        this.forexQueryService = forexQueryService;
    }

    @GetMapping("/pairs")
    @Operation(summary = "获取所有可用的货币对", description = "返回系统中存在的所有货币对列表")
    public ResponseEntity<List<String>> getAllCurrencyPairs() {
        return ResponseEntity.ok(forexQueryService.getAllCurrencyPairs());
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新行情", description = "获取指定货币对的最新行情数据")
    public ResponseEntity<ForexDataDTO> getLatestPrice(
            @Parameter(description = "货币对，如EUR/USD", required = true)
            @RequestParam String currencyPair) {

        return forexQueryService.getLatestPrice(currencyPair)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/range")
    @Operation(summary = "按时间范围查询", description = "查询指定货币对在某个时间范围内的行情数据")
    public ResponseEntity<List<ForexDataDTO>> getPricesInRange(
            @Parameter(description = "货币对，如EUR/USD", required = true)
            @RequestParam String currencyPair,

            @Parameter(description = "开始时间，格式：yyyy-MM-dd HH:mm:ss")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,

            @Parameter(description = "结束时间，格式：yyyy-MM-dd HH:mm:ss")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,

            @Parameter(description = "可选，交易所名称")
            @RequestParam(required = false) String exchange) {

        Instant start = startTime.toInstant(ZoneOffset.UTC);
        Instant end = endTime.toInstant(ZoneOffset.UTC);

        List<ForexDataDTO> result = forexQueryService.getPricesInRange(
                currencyPair, start, end, exchange);

        return ResponseEntity.ok(result);
    }

    @GetMapping("/aggregated")
    @Operation(summary = "获取聚合数据", description = "按指定时间间隔聚合行情数据，如每小时平均值")
    public ResponseEntity<List<ForexDataDTO>> getAggregatedData(
            @Parameter(description = "货币对，如EUR/USD", required = true)
            @RequestParam String currencyPair,

            @Parameter(description = "开始时间，格式：yyyy-MM-dd HH:mm:ss")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,

            @Parameter(description = "结束时间，格式：yyyy-MM-dd HH:mm:ss")
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,

            @Parameter(description = "时间间隔，如1h, 30m, 15s", required = true)
            @RequestParam String interval) {

        Instant start = startTime.toInstant(ZoneOffset.UTC);
        Instant end = endTime.toInstant(ZoneOffset.UTC);

        List<ForexDataDTO> result = forexQueryService.getAggregatedData(
                currencyPair, start, end, interval);

        return ResponseEntity.ok(result);
    }
}

